Ext.namespace('pages.p2p.tjbb.day.query');
pages.p2p.tjbb.day.query.Main = function(config) {
	Ext.apply(this, config);
	this.initUIComponents();
	pages.p2p.tjbb.day.query.Main.superclass.constructor.call(this);
};

Ext.extend(pages.p2p.tjbb.day.query.Main, Ext.Panel, {
	/**
	 * 构造函数
	 */
	initUIComponents : function() {

		this.queryForm = new Ext.form.FormPanel({
					layout : 'column',
					region : 'north',
					labelWidth : 100,
					labelAlign : 'right',
					autoHeight : true,
					border : false,
					frame : false,
					items : [{
								columnWidth : 1,
								height : 10,
								border : false
							}, {
								columnWidth : .25,
								layout : 'form',
								border : false,
								items : [{
											fieldLabel : '统计日期',
											name : 'TJ_DATE',
											id : 'TJ_DATE',
											format : 'Y-m-d',
											anchor : '96%',
											xtype : 'datefield'
										}]
							}]
				});

		this.store = new Ext.data.Store({
					proxy : new Ext.data.HttpProxy({
								url : ExtJ
										.getActionPath('tjbb/queryTjbbDay.do')
							}),
					remoteSort : true,
					reader : new Ext.data.JsonReader({
								root : 'data',
								sumJe : 'sumJe',
								id : 'ID'
							}, [{
										name : 'GSMC',
										mapping : 'GSMC'
									}, {
										name : 'MDMC',
										mapping : 'MDMC'
									}, {
										name : 'TBYZ',
										mapping : 'TBYZ'
									}, {
										name : 'NHYZ',
										mapping : 'NHYZ'
									}, {
										name : 'NHYZ_MONTH',
										mapping : 'NHYZ_MONTH'
									}, {
										name : 'TBYZ_M',
										mapping : 'TBYZ_M'
									}, {
										name : 'NHYZ_M',
										mapping : 'NHYZ_M'
									}, {
										name : 'NHYZ_MONTH_M',
										mapping : 'NHYZ_MONTH_M'
									}, {
										name : 'HBDY',
										mapping : 'HBDY'
									}, {
										name : 'JTBZ',
										mapping : 'JTBZ'
									}, {
										name : 'JNHZ',
										mapping : 'JNHZ'
									}, {
										name : 'JNHZ_MONTH',
										mapping : 'JNHZ_MONTH'
									}, {
										name : 'JTBZ_M',
										mapping : 'JTBZ_M'
									}, {
										name : 'JNHZ_M',
										mapping : 'JNHZ_M'
									}, {
										name : 'JNHZ_MONTH_M',
										mapping : 'JNHZ_MONTH_M'
									}, {
										name : 'TJ_DATE',
										mapping : 'TJ_DATE'
									}])
				});

		this.eug_gs = new Ext.ux.grid.GridSummary();
		var sm = new Ext.grid.CheckboxSelectionModel({
					singleSelect : true
				});
		var cm = new Ext.grid.ColumnModel([sm, new Ext.grid.RowNumberer(), {
			header : '公司名称',
			width : 90,
			sortable : true,
			dataIndex : 'GSMC',
			summaryRenderer : function(val) {
				return '<span style="color:#00000;font-weight:bold;font-size:18px;">合计 :</span>';
			}.createDelegate(this)
		}, {
			header : '门店名称',
			width : 80,
			sortable : true,
			dataIndex : 'MDMC'
		}, {
			header : '当日投标原值',
			width : 150,
			sortable : true,
			dataIndex : 'TBYZ',
			summaryRenderer : function(val) {
				if (null == this.store.reader.jsonData
						|| undefined == this.store.reader.jsonData) {
					return '';
				}
				var sumJe = this.store.reader.jsonData.sumJe;
				if (null == sumJe.TBYZ || undefined == sumJe.TBYZ) {
					return '';
				}
				return '<span style="color:#FF0000;font-weight:bold;font-size:18px;">¥'
						+ Ext.util.Format.substr(Ext.util.Format
										.usMoney(sumJe.TBYZ), 1) + '</span>';
			}.createDelegate(this)
		}, {
			header : '当日年化原值(按天)',
			width : 150,
			sortable : true,
			dataIndex : 'NHYZ',
			summaryRenderer : function(val) {
				if (null == this.store.reader.jsonData
						|| undefined == this.store.reader.jsonData) {
					return '';
				}
				var sumJe = this.store.reader.jsonData.sumJe;
				if (null == sumJe.NHYZ || undefined == sumJe.NHYZ) {
					return '';
				}
				return '<span style="color:#FF0000;font-weight:bold;font-size:18px;">¥'
						+ Ext.util.Format.substr(Ext.util.Format
										.usMoney(sumJe.NHYZ), 1) + '</span>';
			}.createDelegate(this)
		}, {
			header : '当日年化原值(按月)',
			width : 150,
			sortable : true,
			dataIndex : 'NHYZ_MONTH',
			summaryRenderer : function(val) {
				if (null == this.store.reader.jsonData
						|| undefined == this.store.reader.jsonData) {
					return '';
				}
				var sumJe = this.store.reader.jsonData.sumJe;
				if (null == sumJe.NHYZ_MONTH || undefined == sumJe.NHYZ_MONTH) {
					return '';
				}
				return '<span style="color:#FF0000;font-weight:bold;font-size:18px;">¥'
						+ Ext.util.Format.substr(Ext.util.Format
										.usMoney(sumJe.NHYZ_MONTH), 1) + '</span>';
			}.createDelegate(this)
		}, {
			header : '本月投标原值合计',
			width : 150,
			sortable : true,
			dataIndex : 'TBYZ_M',
			summaryRenderer : function(val) {
				if (null == this.store.reader.jsonData
						|| undefined == this.store.reader.jsonData) {
					return '';
				}
				var sumJe = this.store.reader.jsonData.sumJe;
				if (null == sumJe.TBYZ_M || undefined == sumJe.TBYZ_M) {
					return '';
				}
				return '<span style="color:#FF0000;font-weight:bold;font-size:18px;">¥'
						+ Ext.util.Format.substr(Ext.util.Format
										.usMoney(sumJe.TBYZ_M), 1) + '</span>';
			}.createDelegate(this)
		}, {
			header : '本月年化原值合计(按天)',
			width : 150,
			sortable : true,
			dataIndex : 'NHYZ_M',
			summaryRenderer : function(val) {
				if (null == this.store.reader.jsonData
						|| undefined == this.store.reader.jsonData) {
					return '';
				}
				var sumJe = this.store.reader.jsonData.sumJe;
				if (null == sumJe.NHYZ_M || undefined == sumJe.NHYZ_M) {
					return '';
				}
				return '<span style="color:#FF0000;font-weight:bold;font-size:18px;">¥'
						+ Ext.util.Format.substr(Ext.util.Format
										.usMoney(sumJe.NHYZ_M), 1) + '</span>';
			}.createDelegate(this)
		}, {
			header : '本月年化原值合计(按月)',
			width : 150,
			sortable : true,
			dataIndex : 'NHYZ_MONTH_M',
			summaryRenderer : function(val) {
				if (null == this.store.reader.jsonData
						|| undefined == this.store.reader.jsonData) {
					return '';
				}
				var sumJe = this.store.reader.jsonData.sumJe;
				if (null == sumJe.NHYZ_MONTH_M || undefined == sumJe.NHYZ_MONTH_M) {
					return '';
				}
				return '<span style="color:#FF0000;font-weight:bold;font-size:18px;">¥'
						+ Ext.util.Format.substr(Ext.util.Format
										.usMoney(sumJe.NHYZ_MONTH_M), 1) + '</span>';
			}.createDelegate(this)
		}, {
			header : '红包抵用',
			width : 100,
			sortable : true,
			dataIndex : 'HBDY',
			summaryRenderer : function(val) {
				if (null == this.store.reader.jsonData
						|| undefined == this.store.reader.jsonData) {
					return '';
				}
				var sumJe = this.store.reader.jsonData.sumJe;
				if (null == sumJe.HBDY || undefined == sumJe.HBDY) {
					return '';
				}
				return '<span style="color:#FF0000;font-weight:bold;font-size:18px;">¥'
						+ Ext.util.Format.substr(Ext.util.Format
										.usMoney(sumJe.HBDY), 1) + '</span>';
			}.createDelegate(this)
		}, {
			header : '当日净投标',
			width : 150,
			sortable : true,
			dataIndex : 'JTBZ',
			summaryRenderer : function(val) {
				if (null == this.store.reader.jsonData
						|| undefined == this.store.reader.jsonData) {
					return '';
				}
				var sumJe = this.store.reader.jsonData.sumJe;
				if (null == sumJe.JTBZ || undefined == sumJe.JTBZ) {
					return '';
				}
				return '<span style="color:#FF0000;font-weight:bold;font-size:18px;">¥'
						+ Ext.util.Format.substr(Ext.util.Format
										.usMoney(sumJe.JTBZ), 1) + '</span>';
			}.createDelegate(this)
		}, {
			header : '当日净年化(按天)',
			width : 150,
			sortable : true,
			dataIndex : 'JNHZ',
			summaryRenderer : function(val) {
				if (null == this.store.reader.jsonData
						|| undefined == this.store.reader.jsonData) {
					return '';
				}
				var sumJe = this.store.reader.jsonData.sumJe;
				if (null == sumJe.JNHZ || undefined == sumJe.JNHZ) {
					return '';
				}
				return '<span style="color:#FF0000;font-weight:bold;font-size:18px;">¥'
						+ Ext.util.Format.substr(Ext.util.Format
										.usMoney(sumJe.JNHZ), 1) + '</span>';
			}.createDelegate(this)
		}, {
			header : '当日净年化(按月)',
			width : 150,
			sortable : true,
			dataIndex : 'JNHZ_MONTH',
			summaryRenderer : function(val) {
				if (null == this.store.reader.jsonData
						|| undefined == this.store.reader.jsonData) {
					return '';
				}
				var sumJe = this.store.reader.jsonData.sumJe;
				if (null == sumJe.JNHZ_MONTH || undefined == sumJe.JNHZ_MONTH) {
					return '';
				}
				return '<span style="color:#FF0000;font-weight:bold;font-size:18px;">¥'
						+ Ext.util.Format.substr(Ext.util.Format
										.usMoney(sumJe.JNHZ_MONTH), 1) + '</span>';
			}.createDelegate(this)
		}, {
			header : '当月净投标合计',
			width : 150,
			sortable : true,
			dataIndex : 'JTBZ_M',
			summaryRenderer : function(val) {
				if (null == this.store.reader.jsonData
						|| undefined == this.store.reader.jsonData) {
					return '';
				}
				var sumJe = this.store.reader.jsonData.sumJe;
				if (null == sumJe.JTBZ_M || undefined == sumJe.JTBZ_M) {
					return '';
				}
				return '<span style="color:#FF0000;font-weight:bold;font-size:18px;">¥'
						+ Ext.util.Format.substr(Ext.util.Format
										.usMoney(sumJe.JTBZ_M), 1) + '</span>';
			}.createDelegate(this)
		}, {
			header : '当月净年化合计(按天)',
			width : 150,
			sortable : true,
			dataIndex : 'JNHZ_M',
			summaryRenderer : function(val) {
				if (null == this.store.reader.jsonData
						|| undefined == this.store.reader.jsonData) {
					return '';
				}
				var sumJe = this.store.reader.jsonData.sumJe;
				if (null == sumJe.JNHZ_M || undefined == sumJe.JNHZ_M) {
					return '';
				}
				return '<span style="color:#FF0000;font-weight:bold;font-size:18px;">¥'
						+ Ext.util.Format.substr(Ext.util.Format
										.usMoney(sumJe.JNHZ_M), 1) + '</span>';
			}.createDelegate(this)
		}, {
			header : '当月净年化合计(按月)',
			width : 150,
			sortable : true,
			dataIndex : 'JNHZ_MONTH_M',
			summaryRenderer : function(val) {
				if (null == this.store.reader.jsonData
						|| undefined == this.store.reader.jsonData) {
					return '';
				}
				var sumJe = this.store.reader.jsonData.sumJe;
				if (null == sumJe.JNHZ_MONTH_M || undefined == sumJe.JNHZ_MONTH_M) {
					return '';
				}
				return '<span style="color:#FF0000;font-weight:bold;font-size:18px;">¥'
						+ Ext.util.Format.substr(Ext.util.Format
										.usMoney(sumJe.JNHZ_MONTH_M), 1) + '</span>';
			}.createDelegate(this)
		}

		]);

		/**
		 * GridPanel
		 */
		this.grid = new Ext.grid.GridPanel({
					id : 'grid',
					region : 'center',
					store : this.store,
					border : true,
					frame : false,
					plugins : this.eug_gs,
					viewConfig : {
						autoFill : false,
						forceFit : false,
						focusRow : function() {
							return;
						},
						getRowClass : function(record, rowIndex, rowParams,
								store) {
							if (record.get("TOP_LEVEL") == "1") {
								return 'ux-grid-record-onTop';
							}
						}
					},
					sm : sm,
					cm : cm,
					tbar : [{
								id : 'query',
								iconCls : 'query',
								text : '查询',
								handler : this.onQuery.createDelegate(this)
							}, '-', {
								id : 'reset',
								iconCls : 'reset',
								text : '重置',
								handler : this.onReset.createDelegate(this)
							}, '-', {
								id : 'export',
								iconCls : 'excel',
								text : '下载Excel',
								handler : this.onExport.createDelegate(this)
							}, '-', {
								id : 'view',
								iconCls : 'view',
								text : '查看分用户统计',
								handler : this.onView.createDelegate(this)
							}, '-', {
								id : 'viewsybj',
								iconCls : 'view',
								text : '当日收益与本金发放统计',
								handler : this.onViewSyAndBj.createDelegate(this)
							}]
				});

		Ext.apply(this, {
					title : '日报表',
					closeAction : 'close',
					layout : 'border',
					border : false,
					tools : [{
						id : 'help',
						handler : function(){
							this.onHelp();				
						}.createDelegate(this)
					}],
					listeners : {
						'render' : this.initData.createDelegate(this)
					},
					items : [this.queryForm, this.grid]
				})
	},
	
	onHelp : function(){
		var dialog = new global.help.HelpWindow({key : 'zz.all.helpday'});
		dialog.show();
	},

	initData : function() {
		this.onQuery();
	},

	onReset : function() {
		this.queryForm.getForm().reset();
	},

	onQuery : function() {
		this.store.baseParams.tjDate = Ext.getCmp('TJ_DATE').getRawValue();
		var tjDate = this.store.baseParams.tjDate;
		if (tjDate == '') {// 设置默认值为前一天
			var now = new Date().format('Y-m-d');
			var sDate = new Date();
			sDate.setDate(sDate.getDate() - 1);
			this.store.baseParams.tjDate = sDate.format('Y-m-d');
			Ext.getCmp('TJ_DATE').setValue(this.store.baseParams.tjDate);
		}
		var count = this.store.getCount();
		if(count>0){
			this.grid.getStore().removeAll();
			this.store.reader.jsonData.sumJe=0;
			this.eug_gs.refreshSummary();
		}
		Ext.getBody().mask("正在查询,请稍候...");
		this.store.reload({
					callback : function() {
						Ext.getBody().unmask(true);
					}
				});
	},
	onView : function() {
		var record = this.grid.getSelectionModel().getSelected();
		if (record) {
			var win = new pages.p2p.tjbb.day.query.View({
						record : record
					});
			win.show();
		} else {
			Ext.Msg.alert("提示", "请选择一条记录", function() {
						return;
					});
		}
	},

	onExport : function() {
		var tjDate = Ext.getCmp('TJ_DATE').getRawValue();
		ExtJ.doExport({
					url : baseUrl + "/tjbb/exportTjbbDayExcelMethod.do",
					params : {
						tjDate : tjDate
					}
				});
	},
	onViewSyAndBj : function() {
		var tjDate = Ext.getCmp('TJ_DATE').getRawValue();
			var win = new pages.p2p.tjbb.day.query.ViewSyAndBj({
						record : tjDate
					});
			win.show();
	}
});